<?php
namespace Company\Controller;
use Think\Controller;
class CoderuleController extends BaseController {
	
	public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
        C('erp_dbname',$g['db']);
    }
	public function print_code(){
		$text=I('text');
		$this->assign('text',$text);
		$this->display();
	}
    //列表
    public function product(){
		$Product=M('Product', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        $m=M('FactoryRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('LevelRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$cate=M('Category', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$cou=M('Country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['goods_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$Product->where($map)->count();
		$list=$Product->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$factory=$m->where(array('rule_code'=>$v['factory_rule_code']))->find();
			$list[$k]['factory_rule_name']=$factory['name'];
			$level=$n->where(array('rule_code'=>$v['level_rule_code']))->find();
			$list[$k]['level_rule_name']=$level['level'];
			$category=$cate->where(array('name'=>$v['category']))->find();
			$list[$k]['category_rule_code']=$category['rule_code'];
			$country=$cou->where(array('country_name'=>$v['country']))->find();
			$list[$k]['country_rule_code']=$level['rule_code'];
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	//添加、更新信息
	function add_product(){
			$Product=M('Product', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$id=$param['id'];
			$rule_code=$param['rule_code'];
			$count=strlen(trim($rule_code));
			if($count<>4){
				$this->error('请输入4位数字！');
			}
			$res=$Product->where(array('id'=>$id))->save($param);
			// echo $Product->getlastsql();
			// die;
			if($res!==false){
				$this->success('更新成功');
			}

	}
	//编辑
	function edit_product(){
		$id=trim(I('id'));
		$Product=M('Product', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$m=M('FactoryRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('LevelRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$Product->where(array('id'=>$id))->find();
		
		
		$factory_list=$m->select();
		$level_list=$n->select();
		
		$this->assign("factory_list",$factory_list);
		$this->assign("level_list",$level_list);
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除部门
	function del_product(){
		$id=trim(I('id'));
		$Product=M('Product', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		
		$status=$Product->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	
	//列表
    public function category(){
		$m=M('Category', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['goods_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	//添加、更新信息
	function add_category(){
			$m=M('Category', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$id=$param['id'];
			$rule_code=$param['rule_code'];
			$count=strlen(trim($rule_code));
			if($count<>4){
				$this->error('请输入4位数字！');
			}
			$res=$m->where(array('id'=>$id))->save($param);
			// echo $m->getlastsql();
			// die;
			if($res!==false){
				$this->success('更新成功');
			}

	}
	//编辑
	function edit_category(){
		$id=trim(I('id'));
		$m=M('Category', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_category(){
		$id=trim(I('id'));
		$m=M('Category', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//列表
    public function country(){
		$m=M('Country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['goods_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	//添加、更新信息
	function add_country(){
			$m=M('Country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$id=$param['id'];
			$rule_code=$param['rule_code'];
			$count=strlen(trim($rule_code));
			if($count<>4){
				$this->error('请输入4位数字！');
			}
			$res=$m->where(array('id'=>$id))->save($param);
			// echo $m->getlastsql();
			// die;
			if($res!==false){
				$this->success('更新成功');
			}

	}
	//编辑
	function edit_country(){
		$id=trim(I('id'));
		$m=M('Country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_country(){
		$id=trim(I('id'));
		$m=M('Country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();
		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//列表
    public function factory(){
		$m=M('FactoryRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['goods_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	//添加、更新信息
	function add_factory(){
		if(IS_POST){
			$m=M('FactoryRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$id=$param['id'];
			$rule_code=$param['rule_code'];
			$count=strlen(trim($rule_code));
			if($count<>4){
				$this->error('请输入4位数字！');
			}
			$res=$m->where(array('id'=>$id))->save($param);
			// echo $m->getlastsql();
			// die;
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//添加
	function add_factorys(){
			$m=M('FactoryRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$name=$param['name'];
			$rule_code=$param['rule_code'];
			$count=strlen(trim($rule_code));
			if($count<>4){
				$this->error('请输入4位数字！');
			}
			$info=$m->where(array('rule_code'=>$rule_code))->find();
			if($info){
				$this->error('编码规则重复');
			}else{
			$res=$m->add($param);
			
			if($res!==false){
				$this->success('更新成功');
			}
			}

	}
	//编辑
	function edit_factory(){
		$id=trim(I('id'));
		$m=M('FactoryRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_factory(){
		$id=trim(I('id'));
		$m=M('FactoryRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();
		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	
	//列表
    public function level(){
		$m=M('LevelRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['goods_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	//添加、更新信息
	function add_level(){
		if(IS_POST){
			$m=M('LevelRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$id=$param['id'];
			$rule_code=$param['rule_code'];
			$count=strlen(trim($rule_code));
			if($count<>4){
				$this->error('请输入4位数字！');
			}
			$res=$m->where(array('id'=>$id))->save($param);
			// echo $m->getlastsql();
			// die;
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}

	}
	function add_levels(){
			$m=M('LevelRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$level=$param['level'];
			$rule_code=$param['rule_code'];
			$count=strlen(trim($rule_code));
			if($count<>4){
				$this->error('请输入4位数字！');
			}
			$info=$m->where(array('rule_code'=>$rule_code))->find();
			if($info){
				$this->error('编码规则重复');
			}else{
			$res=$m->add($param);

			if($res!==false){
				$this->success('更新成功');
			}
			}

	}
	//编辑
	function edit_level(){
		$id=trim(I('id'));
		$m=M('LevelRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_level(){
		$id=trim(I('id'));
		$m=M('LevelRule', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();
		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
}